SHELL=/bin/sh
######################################################################
# GenOpt Makefile.
# This file is only needed by developers.
# It can be used to compile, document and run unit tests
# from a bash shell.
#
# MWetter@lbl.gov                                           2008-05-08
######################################################################
UNAME = $(shell uname)

# List of source code files
SRC=`find genopt -name '*.java'`
# List of class files
CLAFIL=`find src -name '*.class'`
CLAFILGO=`find genopt -name '*.class'`
# List of run time files
RUNFIL=`find example \( -name 'OutputListing*' -or -name 'GenOpt.log' \
        -or -name 'eplusout.*' -or -name '*.eso' -or -name '*.audit' \
        -or -name '*.err' -or -name '*.eio' -or -name '*.end' -or -name '*.csv' \
        -or -name '*.svg' -or -name '*.bnd' -or -name '*.mtd' \
        -or -name '*.audit' \)`
RUNDIR=`find example \( -name 'Output' -or -name 'tmp-genopt-run*' \)`

RUNDYMFIL=`find example/dymola \( -name 'dsres.mat' \
	-or -name 'dslog.txt' -or -name 'dsmodel.c' -or -name 'dsfinal.txt' \
	-or -name 'buildlog.txt' -or -name 'ObjectiveFunction.mat' \
	-or -name 'request.' -or -name 'status'  -or -name 'success' \)`

# Directory where distributable will be copied
DISDIR=dist

# List of directories with example files
EXADIR=$(shell find example/quad -maxdepth 1 \( -type d -not -name '.svn' \) )
EXADIRCON=$(shell find example/quad/constraints -maxdepth 1 \( -type d -not -name '.svn' \) )

# Root directory of GenOpt
ROODIR=$(shell pwd)

# Initialization file for examples


ifeq ($(UNAME), Darwin)
INIFIL=optMacOSX.ini
endif
ifeq ($(UNAME), Linux)
INIFIL=optLinux.ini
endif
ifeq ($(UNAME), CYGWIN_NT-5.1)
INIFIL=optWinXP.ini
endif


# Executable for izpack. IzPack is used
# to create the GenOpt installation software
ifeq ($(UNAME), Darwin)
IZPACK=/Applications/IzPack/bin/compile
else
IZPACK=izpack
endif

#export genopt-version-dot=3.0.0-beta2
export genopt-version-dot=3.1.1

export genopt-version-dash=$(shell echo $(genopt-version-dot) | sed -e 's/[.]/-/g')
### Targets of Makefile ###########################
### Compiles the GenOpt source code and the JavaDoc
all: doc jar

### Compiles the GenOpt source code
prog:
	(cd src; javac -Xlint:unchecked $(SRC))
	@echo "==== Made program"

### Makes the jar file
jar: prog
	(cd src; jar cfm ../genopt.jar genopt/Manifest.txt $(CLAFILGO) genopt/img/* ../legal.html)
	jar -i genopt.jar
	@echo "==== Made jar file"

### Deletes autogenerated files
clean:
	@if [ "$(CLAFIL)"    != "" ]; then rm -v $(CLAFIL); fi
	@if [ "$(RUNFIL)"    != "" ]; then rm -v $(RUNFIL); fi
	@if [ "$(RUNDIR)"    != "" ]; then rm -r $(RUNDIR); fi
	@if [ "$(RUNDYMFIL)" != "" ]; then rm -v $(RUNDYMFIL); fi
	@echo "==== Deleted autogenerated files"

### Generate the docs
doc:  doc-latex doc-java
### Generated the manual
doc-latex:
	rm -f src/manual/manual.pdf
	rm -f documentation/manual.pdf
	(cd src/manual; ./run.sh)
	cp -v src/manual/manual.pdf documentation
	@echo "==== Generated manual documentation"

### generates JavaDoc html files
doc-java:
	rm -f documentation/jdoc/*.html documentation/jdoc/package-list
	rm -rf documentation/jdoc/genopt/*
	(cd src; javadoc -Xdoclint:none -breakiterator -private -author -version \
	-windowtitle "GenOpt Code Documentation" \
	-bottom "<DIV CLASS="FOOTER"> <P> <CENTER> <A HREF="http://simulationresearch.lbl.gov/GO" \
	TARGET=_top>GenOpt</A> | <A HREF="http://simulationresearch.lbl.gov" \
	TARGET=_top>LBL SRG</A> | <A HREF="http://www.lbl.gov" \
	TARGET=_top>LBL</A> </CENTER> <HR WIDTH="100%"> \
	<BR> Contact: <A HREF="mailto:MWetter@lbl.gov">MWetter@lbl.gov</A> </DIV>" -d ../documentation/jdoc $(SRC))
	@echo "==== Made Java documentation"

### Copies the files to the web directory
www:
	cp documentation/manual.pdf ~/proj/www/genopt/GO/download/manual-$(genopt-version-dash).pdf
	cp $(DISDIR)/genopt-install-$(genopt-version-dash).jar ~/proj/www/genopt/GO/download
	rm -rf ~/proj/www/genopt/GO/jdoc
	mkdir ~/proj/www/genopt/GO/jdoc
	cp documentation/jdoc/*.html ~/proj/www/genopt/GO/jdoc/
	cp documentation/jdoc/*.css ~/proj/www/genopt/GO/jdoc/
	cp documentation/jdoc/package-list ~/proj/www/genopt/GO/jdoc/
	cp -r documentation/jdoc/genopt ~/proj/www/genopt/GO/jdoc/
	@echo "==== Copied files to web directory ~/proj/www/genopt/GO"

### unit tests
unitTest:
	@if [ ! -d unitTestResults ]; then \
	  echo "Error. Directory unitTestResults does not exist."; \
	  echo "Check out directory from version control before"; \
	  echo "running 'make unitTest'"; \
	  exit 1;\
	fi;
	@for x in $(EXADIR) $(EXADIRCON); do \
	    cd "$(ROODIR)"; \
	    echo "++++ $$x"; \
	    if [ -f $$x/${INIFIL} ]; then \
		if [ ${UNAME} != CYGWIN_NT-5.1 ]; then \
		       	java -ea -classpath genopt.jar genopt.GenOpt $$x/${INIFIL}; \
		else \
		       	java -ea -classpath genopt.jar genopt.GenOpt `cygpath -w $$x/${INIFIL}`; \
		fi;\
		tesNam=`echo $$x | sed 's|/|-|g'`; \
		sed 1,19d $$x/OutputListingAll.txt > unitTestResults/$$tesNam.txt; \
	        if [ "$$?" != "0" ]; then \
	            echo "*** Error: Unit test failed for $$x/${INIFIL}"; \
	            exit 1; fi; \
	    else \
	          echo "*** Nothing to run in $$x"; \
	    fi; \
	done; \
	git status unitTestResults
	@echo "==== Finished unit tests";

### install program
inst:
	mkdir -p $(DISDIR)
	rm -f $(DISDIR)/genopt-install.jar
	rm -f $(DISDIR)/genopt-install-$(genopt-version-dash).jar
	cd install; \
	$(IZPACK) install.xml -b . -o genopt-install-$(genopt-version-dash).jar -k standard; \
	mv genopt-install-$(genopt-version-dash).jar ../$(DISDIR);
	@echo "==== Made installation program in $(DISDIR)/genopt-install-$(genopt-version-dash).jar";

### distribution, including web pages
dist:   clean prog doc jar inst www
	@echo "==== Made distribution";
